package com.example.pinkcat.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.LoginRegisterPage;
import com.example.pinkcat.activity.SeachPage;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.adapter.Tuijianye_FragmentPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.rishabhharit.roundedimageview.RoundedImageView;

public class Fragment_shouye extends Fragment {
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private Tuijianye_FragmentPagerAdapter tuijianye_fragmentPagerAdapter;

    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private TabLayout.Tab three;
    private TabLayout.Tab four;
    private TabLayout.Tab five;

    NavController navController;

    RoundedImageView roundedImageView_head;
    Context context;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shouye,container,false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!userStaticMsg.head_img.equals("null"))
            Glide.with(context).load(Uri.parse(userStaticMsg.head_img)).into(roundedImageView_head);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        roundedImageView_head=view.findViewById(R.id.tuijian_tx);
        context=view.getContext();
        //使用适配器将ViewPager与Fragment绑定在一起
        mViewPager = view.findViewById(R.id.viewPage);
        tuijianye_fragmentPagerAdapter = new Tuijianye_FragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(tuijianye_fragmentPagerAdapter);

        //将TabLayout与ViewPager绑定在一起
        mTablayout =view.findViewById(R.id.tablayout);
        mTablayout.setupWithViewPager(mViewPager);

        //指定Tab的位置
        one =  mTablayout.getTabAt(0); //推荐页
        two =  mTablayout.getTabAt(1); //热门
        three = mTablayout.getTabAt(2); //追番
        four = mTablayout.getTabAt(3); //影视
        five = mTablayout.getTabAt(4); //分区

        view.findViewById(R.id.tuijian_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(view.getContext(), SeachPage.class);
                view.getContext().startActivity(intent);
            }
        });

        //点击跳转个人页面
        roundedImageView_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userStaticMsg.login){
                    Intent intent=new Intent(view.getContext(), Userpage.class);
                    intent.putExtra("up_id",userStaticMsg.userId);
                    view.getContext().startActivity(intent);
                }
                else {
                    Toast.makeText(view.getContext(),"请登陆!",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(view.getContext(), LoginRegisterPage.class);
                    view.getContext().startActivity(intent);
                }

            }
        });

    }
}
